﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="SooritaKannev2", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(SooritaKannev2))]
    public class SooritaKannev2Serializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected ISooritaKannev2 clazz;
        
        public SooritaKannev2Serializer(ISooritaKannev2 clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("kande_id");
            if (clazz.KandeId!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.KandeId))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : KandeId");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("kandeliik");
            if (clazz.Kandeliik!=null) {
                Writer.WriteTypeAttribute("http://producers.arireg.xtee.riik.ee/producer/arireg", "x_kandeliik");
                Writer.WriteString(clazz.Kandeliik.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Kandeliik");
            }
            Writer.WriteEndElement();
            if (clazz.Seisund!=null) {
                Writer.WriteStartElement("seisund");
                global::Xtee.Core.SerializationMetadata metaSeisund = this.GetMetaData(clazz.Seisund);
                Writer.WriteTypeAttribute(metaSeisund.TypeQualifiedName.Namespace, metaSeisund.TypeQualifiedName.Name);
                metaSeisund.NewSerializer(clazz.Seisund).WriteXml(Writer);
                Writer.WriteEndElement();
            }
            if (clazz.EelmineLahend!=null) {
                Writer.WriteStartElement("eelmine_lahend");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.EelmineLahend))).ToString());
                Writer.WriteEndElement();
            }
            Writer.WriteStartElement("kande_kuupaev");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "date");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDate(((System.DateTime)(clazz.KandeKuupaev))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("ametitoiming");
            if (clazz.Ametitoiming!=null) {
                global::Xtee.Core.SerializationMetadata metaAmetitoiming = this.GetMetaData(clazz.Ametitoiming);
                Writer.WriteTypeAttribute(metaAmetitoiming.TypeQualifiedName.Namespace, metaAmetitoiming.TypeQualifiedName.Name);
                metaAmetitoiming.NewSerializer(clazz.Ametitoiming).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Ametitoiming");
            }
            Writer.WriteEndElement();
            if (clazz.Riigiloivud!=null) {
                Writer.WriteStartElement("riigiloivud");
                Writer.WriteTypeAttribute("http://schemas.xmlsoap.org/soap/encoding/", "Array");
                Writer.WriteArrayTypeAttribute("riigiloivType", "http://producers.arireg.xtee.riik.ee/producer/arireg", clazz.Riigiloivud.Count);
                if (((clazz.Riigiloivud == null) 
                            || (clazz.Riigiloivud.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                for (int x = 0; ((clazz.Riigiloivud != null) 
                            && (x < clazz.Riigiloivud.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("item");
                    if (clazz.Riigiloivud[x]!=null) {
                        global::Xtee.Core.SerializationMetadata metaRiigiloivud = this.GetMetaData(clazz.Riigiloivud[x]);
                        Writer.WriteTypeAttribute(metaRiigiloivud.TypeQualifiedName.Namespace, metaRiigiloivud.TypeQualifiedName.Name);
                        metaRiigiloivud.NewSerializer(clazz.Riigiloivud[x]).WriteXml(Writer);
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
                Writer.WriteEndElement();
            }
            if (clazz.KandeSisu!=null) {
                Writer.WriteStartElement("kande_sisu");
                global::Xtee.Core.SerializationMetadata metaKandeSisu = this.GetMetaData(clazz.KandeSisu);
                Writer.WriteTypeAttribute(metaKandeSisu.TypeQualifiedName.Namespace, metaKandeSisu.TypeQualifiedName.Name);
                metaKandeSisu.NewSerializer(clazz.KandeSisu).WriteXml(Writer);
                Writer.WriteEndElement();
            }
            if (clazz.Markus!=null) {
                Writer.WriteStartElement("markus");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Markus))).ToString());
                Writer.WriteEndElement();
            }
            if (clazz.Dokumendid!=null) {
                Writer.WriteStartElement("dokumendid");
                Writer.WriteTypeAttribute("http://schemas.xmlsoap.org/soap/encoding/", "Array");
                Writer.WriteArrayTypeAttribute("dokumentType", "http://producers.arireg.xtee.riik.ee/producer/arireg", clazz.Dokumendid.Count);
                if (((clazz.Dokumendid == null) 
                            || (clazz.Dokumendid.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                for (int x = 0; ((clazz.Dokumendid != null) 
                            && (x < clazz.Dokumendid.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("item");
                    if (clazz.Dokumendid[x]!=null) {
                        global::Xtee.Core.SerializationMetadata metaDokumendid = this.GetMetaData(clazz.Dokumendid[x]);
                        Writer.WriteTypeAttribute(metaDokumendid.TypeQualifiedName.Namespace, metaDokumendid.TypeQualifiedName.Name);
                        metaDokumendid.NewSerializer(clazz.Dokumendid[x]).WriteXml(Writer);
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
                Writer.WriteEndElement();
            }
            if (clazz.RegistriPiirkond!=null) {
                Writer.WriteStartElement("registri_piirkond");
                Writer.WriteTypeAttribute("http://producers.arireg.xtee.riik.ee/producer/arireg", "x_regpiirkond");
                Writer.WriteString(clazz.RegistriPiirkond.ToString());
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="kande_id") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>kande_id");
                }
                clazz.KandeId = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("kande_id")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKannev2>kande_id", Reader.LocalName);
            }
            if (Reader.LocalName=="kandeliik") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>kandeliik");
                }
                clazz.Kandeliik = new global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.XKandeliik(Reader.ReadElementString("kandeliik"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKannev2>kandeliik", Reader.LocalName);
            }
            if (Reader.LocalName=="seisund") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>seisund");
                }
                global::Xtee.Core.SerializationMetadata metaSeisund = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISeisundType>(Reader, "seisundType", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.Seisund = metaSeisund.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISeisundType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("seisund");
                    metaSeisund.NewSerializer(clazz.Seisund).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="eelmine_lahend") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>eelmine_lahend");
                }
                clazz.EelmineLahend = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("eelmine_lahend")).Value;
            }
            if (Reader.LocalName=="kande_kuupaev") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>kande_kuupaev");
                }
                clazz.KandeKuupaev = new global::Xtee.Core.Schema.XsdDate(Reader.ReadElementString("kande_kuupaev")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKannev2>kande_kuupaev", Reader.LocalName);
            }
            if (Reader.LocalName=="ametitoiming") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>ametitoiming");
                }
                global::Xtee.Core.SerializationMetadata metaAmetitoiming = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneAmetitoiming>(Reader, "sooritakanne_ametitoiming", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.Ametitoiming = metaAmetitoiming.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneAmetitoiming>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("ametitoiming");
                    metaAmetitoiming.NewSerializer(clazz.Ametitoiming).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKannev2>ametitoiming", Reader.LocalName);
            }
            if (Reader.LocalName=="riigiloivud") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>riigiloivud");
                }
                clazz.Riigiloivud = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("riigiloivud");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.LocalName=="item") {
                            if ((Reader.IsElementNil() == false)) {
                                global::Xtee.Core.SerializationMetadata metaRiigiloivud = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivType>(Reader, "riigiloivType", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                                global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivType item = metaRiigiloivud.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivType>();
                                clazz.Riigiloivud.Add(item);
                                if ((Reader.IsEmptyElement == false)) {
                                    Reader.ReadStartElement("item");
                                    metaRiigiloivud.NewSerializer(item).ReadXml(Reader);
                                    Reader.ReadEndElement();
                                }
                                else {
                                    Reader.Skip();
                                }
                            }
                            else {
                                clazz.Riigiloivud.Add(null);
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">item", Reader.LocalName);
                        }
                    }
                    if ((clazz.Riigiloivud.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="kande_sisu") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>kande_sisu");
                }
                global::Xtee.Core.SerializationMetadata metaKandeSisu = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneKandesisuv2>(Reader, "sooritakanne_kandesisuv2", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.KandeSisu = metaKandeSisu.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneKandesisuv2>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("kande_sisu");
                    metaKandeSisu.NewSerializer(clazz.KandeSisu).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="markus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>markus");
                }
                clazz.Markus = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("markus")).Value;
            }
            if (Reader.LocalName=="dokumendid") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>dokumendid");
                }
                clazz.Dokumendid = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IDokumentType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("dokumendid");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.LocalName=="item") {
                            if ((Reader.IsElementNil() == false)) {
                                global::Xtee.Core.SerializationMetadata metaDokumendid = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IDokumentType>(Reader, "dokumentType", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                                global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IDokumentType item = metaDokumendid.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IDokumentType>();
                                clazz.Dokumendid.Add(item);
                                if ((Reader.IsEmptyElement == false)) {
                                    Reader.ReadStartElement("item");
                                    metaDokumendid.NewSerializer(item).ReadXml(Reader);
                                    Reader.ReadEndElement();
                                }
                                else {
                                    Reader.Skip();
                                }
                            }
                            else {
                                clazz.Dokumendid.Add(null);
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">item", Reader.LocalName);
                        }
                    }
                    if ((clazz.Dokumendid.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="registri_piirkond") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKannev2>registri_piirkond");
                }
                clazz.RegistriPiirkond = new global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.XRegpiirkond(Reader.ReadElementString("registri_piirkond"));
            }
        }
    }
}
